package com.cxj.musicplayer.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cxj.musicplayer.R;
import com.cxj.musicplayer.bean.Song;
import com.cxj.musicplayer.data.DataManager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class LocalSongListAdapter extends BaseAdapter {
    private Context mContext;
    private List<Song> mData;
    public LocalSongListAdapter(Context context, List<Song> data){
        mContext = context;
        mData = data;

        DataManager.array_data.remove("l_song_name");
        DataManager.array_data.remove("l_author_name");
        DataManager.array_data.remove("l_song_id");

        ArrayList<String> mSongName = new ArrayList<>();
        ArrayList<String> mAuthorName = new ArrayList<>();
        ArrayList<String> mSongId = new ArrayList<>();

        if(mData != null){
            for(Song s: mData){
                mSongName.add(s.getSong_name());
                mAuthorName.add(s.getAuthor());
                mSongId.add(s.getSong_id());
            }

            String[] mmSongName = new String[mSongName.size()];
            String[] mmAuthorName  = new String[mAuthorName.size()];
            String[] mmSongId = new String[mSongId.size()];


            DataManager.array_data.put("l_song_name", mSongName.toArray(mmSongName));
            DataManager.array_data.put("l_author_name", mAuthorName.toArray(mmAuthorName));
            DataManager.array_data.put("l_song_id", mSongId.toArray(mmSongId));
        }

    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int i) {
        return mData == null ? null : mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        SongListAdapter.ViewHolder viewHolder;
        if(view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.song_list, null);
            viewHolder = new SongListAdapter.ViewHolder();
            viewHolder.song_name = view.findViewById(R.id.song_name);
            viewHolder.author = view.findViewById(R.id.author);
            viewHolder.position = view.findViewById(R.id.position);
            view.setTag(viewHolder);
        } else {
            viewHolder = (SongListAdapter.ViewHolder) view.getTag();
        }
        String songName = mData.get(i).getSong_name();
        String authorName = mData.get(i).getAuthor().length() > 14 ? mData.get(i).getAuthor().substring(0, 14) + "..." : mData.get(i).getAuthor();
        viewHolder.song_name.setText(songName);
        viewHolder.author.setText(authorName);
        viewHolder.position.setText(String.valueOf(i + 1));
        return view;
    }

    static class ViewHolder{
        TextView song_name;
        TextView position;
        TextView author;
    }
}
